﻿using BaseBll;
using BaseWebApi.App_Start;
using BaseWebApi.Common;
using BaseWebApi.Models;
using Common;
using Models;
using System;
using System.Net.Http;
using System.Web.Http;

namespace BaseWebApi.Controllers
{
    /// <summary>
    /// 登录
    /// </summary>
    [ControllerGroup(groupName: "Login", useage: "用户登录")]
    [WebApiExceptionFilter]
    public class LoginController : ApiController
    {
        private readonly UserBll user_instance;
        /// <summary>
        /// 构造方法
        /// </summary>
        public LoginController()
        {
            user_instance = UserBll.Instance;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userAccount">用户账号</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage Login(string userAccount, string password)
        {
            string description = "登录系统";
            JWTPlayloadInfo playload = new JWTPlayloadInfo();

            try
            {               
                Users model = user_instance.CheckAccount(userAccount, password, out string msg);
                if (model != null)
                {
                    //string md5Password = Common.Utils.MD5(password);
                    // 验证密码
                    bool isValid_password = BCrypt.Net.BCrypt.Verify(password, model.UserPwd);
                    if (isValid_password)
                    {
                        if (model.AccountState == 2)
                        {
                            msg = "该账户已被禁用！";
                            return JsonHelper.FailJson(msg);
                        }
                        if (model.AuditState != 1)
                        {
                            msg = "该账户未认证！";
                            return JsonHelper.FailJson(msg);
                        }
                        if (model.RoleId <= 0)
                        {
                            msg = "该账户未设置权限！";
                            return JsonHelper.FailJson(msg);
                        }

                        //Role role = new RoleBll().GetModel((int)model.RoleId);
                        //生成token
                        //设置过期时间 
                        double seconds = 43200; // 过期秒数(12小时)
                        double exp = (DateTime.UtcNow.AddSeconds(seconds) - new DateTime(1970, 1, 1)).TotalSeconds;

                        // 修改登录时间,登录ip
                        //if (model.LoginTime != null)
                        //{
                        //    model.PrevLoginTime = model.LoginTime;
                        //}
                        //if (!string.IsNullOrEmpty(model.LoginIP))
                        //{
                        //    model.PrevLoginIP = model.LoginIP;
                        //}

                        model.LastLoginTime = DateTime.Now;

                        model.LastLoginIP = Utils.GetClientIP();

                        user_instance.UpdateLogin(model);
                        playload.UserAccount = model.UserAccount;
                        playload.UserName = model.UserName;
                        playload.CompanyId = model.CompanyId;
                        playload.CompanyName = model.CompanyName;
                        playload.DeptId = model.DeptId;
                        playload.DeptName = model.DeptName;
                        playload.IsAdmain = model.IsAdmain;
                        playload.RoleId = model.RoleId;
                        playload.RoleName = model.RoleName;
                        playload.LoginTime = model.LastLoginTime;
                        playload.LoginIP = model.LastLoginIP;
                        playload.exp = exp;
                        var token = JWTHelper.GetToken(playload);

                        // 获取该用户的授权菜单
                        //List<Menu> menuList;
                        //MenuBll mbll = new MenuBll();
                        //if (role.IsAdmain == 1)
                        //{
                        //    //管理员，获取全部菜单
                        //    menuList = mbll.GetListModels(" and MenuState=1");
                        //}
                        //else
                        //{
                        //    menuList = bll.GetRoleMenus((int)model.RoleId);
                        //}

                        // 添加日志
                        LogHelper.LogAction(playload, Constant.SysModule.Login, Constant.ActionEnum.Login, description);
                        return JsonHelper.SuccessLogin("登录成功！", JsonHelper.ModelToJObject(model), null, token);

                    }
                    else
                    {
                        msg = "密码错误！";
                        return JsonHelper.FailJson(msg);
                    }
                    
                }
                else
                {
                    // 添加日志
                    LogHelper.LogWarn(playload, Constant.SysModule.Login, Constant.ActionEnum.Login, description + ":" + msg);
                    return JsonHelper.FailJson(msg);
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogError(playload, ex, Constant.SysModule.Login, Constant.ActionEnum.Login, description);
                throw;
            }

        }
    }
}
